#include <simulator/targetMachine/registerMap.h>

bool RegisterMap::addRegister16(std::shared_ptr<Register<u16>> reg) {
    if (m_reg16Map.find(reg->getRegName()) != m_reg16Map.end())
        return false;
    m_reg16Map[reg->getRegName()] = reg;
    return true;
}

bool RegisterMap::addRegister32(std::shared_ptr<Register<u32>> reg) {
    if (m_reg32Map.find(reg->getRegName()) != m_reg32Map.end())
        return false;
    m_reg32Map[reg->getRegName()] = reg;
    return true;
}

bool RegisterMap::addRegister64(std::shared_ptr<Register<u64>> reg) {
    if (m_reg64Map.find(reg->getRegName()) != m_reg64Map.end())
        return false;
    m_reg64Map[reg->getRegName()] = reg;
    return true;
}

std::shared_ptr<Register<u16>> RegisterMap::getReg16ByName(const std::string &regName) {
    auto targetReg = m_reg16Map.find(regName);
    if (targetReg != m_reg16Map.end())
        return targetReg->second;
    return nullptr;
}

std::shared_ptr<Register<u32>> RegisterMap::getReg32ByName(const std::string &regName) {
    auto targetReg = m_reg32Map.find(regName);
    if (targetReg != m_reg32Map.end())
        return targetReg->second;
    return nullptr;
}

std::shared_ptr<Register<u64>> RegisterMap::getReg64ByName(const std::string &regName) {
    auto targetReg = m_reg64Map.find(regName);
    if (targetReg != m_reg64Map.end())
        return targetReg->second;
    return nullptr;
}

RegisterMap::RegisterMap(const RegisterMap &registerMap) {
    for (auto &register16: registerMap.m_reg16Map) {
        auto newReg = std::make_shared<Register<u16>>(*register16.second);
        m_reg16Map[register16.first] = newReg;
    }

    for (auto &register32: registerMap.m_reg32Map) {
        auto newReg = std::make_shared<Register<u32>>(*register32.second);
        m_reg32Map[register32.first] = newReg;
    }

    for (auto register64: registerMap.m_reg64Map) {
        auto newReg = std::make_shared<Register<u64>>(*register64.second);
        m_reg64Map[register64.first] = newReg;
    }
}
